//
//  HYMeetingScheduleVC.m
//  Booking
//
//  Created by Duntech on 2018/4/19.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYMeetingScheduleVC.h"
#import "HYDateCitySelectView.h"
#import "HYConfirmBookVC.h"
#import "HYSeatBookingDetailVC.h"
#import "HYRoomScheduleCell.h"
#import "HYTouchTableView.h"
#import "HYMyScheduleMeetingRoomModel.h"
#import "HYReservedDetailView.h"

typedef enum : NSUInteger {
    InUpView = 0,
    InDownView,
    InDragView,
    InTableView,
    InOtherView,
} TouchType;

@interface HYMeetingScheduleVC ()<PGDatePickerDelegate,UITableViewDelegate,UITableViewDataSource,TouchTableViewDelegate>
{
    CGFloat _cellHeight;
    int _maxIndex;
    int _minIndex;
}
@property (nonatomic, strong) UIView *navigationView;
@property (nonatomic, strong) UILabel *roomNameLab;
@property (nonatomic, strong) UILabel *dateLab;
@property (nonatomic, strong) UIImageView *searchImg;
@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) HYTouchTableView *tableview;
@property (nonatomic, strong) NSMutableArray *roomDataSource;
@property (nonatomic, strong) NSMutableArray *reservedViewMArr;
@property (nonatomic, strong) NSMutableArray *reservedIndexMArr;

@property (nonatomic, strong) UIImageView *upView;
@property (nonatomic, strong) UIImageView *downView;
@property (nonatomic, strong) UIView *dragView;
@property (nonatomic, strong) UILabel *reserveTimeLab;

@property (nonatomic, strong) NSMutableArray *timeMArr;
@property (nonatomic, assign) TouchType touchType;
@property (nonatomic, assign) CGPoint beginPoint;
@property (nonatomic, assign) CGPoint touchViewPoint;

@end

@implementation HYMeetingScheduleVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _cellHeight = 70.0;
    _minIndex = 0;
    _maxIndex = 48;
    self.startNum = (int)[self.timeMArr indexOfObject:self.startTime];
    self.endNum = (int)[self.timeMArr indexOfObject:self.endTime];
    [self setupSubviews];
    
    if (self.scheduleType == MeetingRoomSchedule) {
        self.title = NSLocalizedString(@"roomSchedule", nil);
        self.navigationView.mj_h = 0;
        [self requestNetwork];
    }
    else if (self.scheduleType == SeatSchedule) {
        self.title = NSLocalizedString(@"seatSchedule", nil);
        self.navigationView.mj_h = 0;
    }
    else{
        self.title = NSLocalizedString(@"seatSchedule", nil);
    }
}

-(void)setupSubviews{
    [self.view addSubview:self.navigationView];
    [self.view addSubview:self.roomNameLab];
    [self.view addSubview:self.dateLab];
    [self.view addSubview:self.searchImg];
    [self.view addSubview:self.confirmBtn];
    [self.view addSubview:self.tableview];
    [self.tableview addSubview:self.dragView];
    [self.tableview addSubview:self.upView];
    [self.tableview addSubview:self.downView];
    [self.dragView addSubview:self.reserveTimeLab];
    
    [self.roomNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.navigationView.mas_bottom);
        make.height.mas_equalTo(44*WIDTH_MULTIPLE);
    }];
    
    [self.dateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.roomNameLab.mas_bottom);
        make.height.mas_equalTo(44*WIDTH_MULTIPLE);
    }];
    
    [self.searchImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).mas_offset(15);
        make.centerY.mas_equalTo(self.dateLab.mas_centerY);
    }];
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).mas_offset(10*WIDTH_MULTIPLE);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-10*WIDTH_MULTIPLE);
        make.bottom.mas_equalTo(self.view.bottom).mas_offset(-24*WIDTH_MULTIPLE);
        make.height.mas_equalTo(50*WIDTH_MULTIPLE);
    }];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.dateLab.mas_bottom);
        make.bottom.mas_equalTo(self.confirmBtn.mas_top).mas_offset(-10);
    }];
    
    [self.reserveTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.dragView.mas_left).mas_offset(45);
        make.top.mas_equalTo(10);
    }];
}

-(void)setRoomModel:(HYMeetingRoomModel *)roomModel{
    _roomModel = roomModel;
    
    self.roomNameLab.text = roomModel.name;
}

-(void)setSeatModel:(HYSeatCellModel *)seatModel{
    _seatModel = seatModel;
    
    self.roomNameLab.text = seatModel.name;
}

#pragma mark -- private method

-(void)requestNetwork{
    kWeakSelf(weakSelf);
    [weakSelf.roomDataSource removeAllObjects];
    [HYNetRequestHandle getMyScheduleRoomWithCreateBy:nil status:nil city:nil build:nil floor:nil startTime:nil endTime:nil markName:nil meetingroomId:self.roomModel.id Complection:^(NSArray *myScheduleList) {
        
        [myScheduleList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            HYMyScheduleMeetingRoomModel *model = [HYMyScheduleMeetingRoomModel modelWithDictionary:obj];
            if ([model.signStatus intValue] != 3) {
                [weakSelf.roomDataSource addObject:model];
            }
        }];
        
        [weakSelf getCurrentDateReserveList];
    }];
}

-(void)getCurrentDateReserveList{
    for (UIView *view in self.reservedViewMArr) {
        if (view.superview != nil) {
            [view removeFromSuperview];
        }
    }
    
    for (HYMyScheduleMeetingRoomModel *model in self.roomDataSource) {
        if ([self.meetingDate isEqualToString:model.currdate]) {
            NSString *startHour = [model.startDayTime substringWithRange:NSMakeRange(0, 5)];
            NSString *endHour = [model.endDayTime substringWithRange:NSMakeRange(0, 5)];
            
            NSUInteger startIndex = [self.timeMArr indexOfObject:startHour];
            NSUInteger endIndex = [self.timeMArr indexOfObject:endHour];
            
            [self.reservedIndexMArr addObject:@{@"startIndex":@(startIndex),@"endIndex":@(endIndex)}];
            
            //计算当前可预约选中的时间段
            if (self.startNum >= startIndex) {
                self.startNum = (int)endIndex;
                self.endNum = self.startNum+1;
                self.startTime = self.timeMArr[self.startNum];
                self.endTime = self.timeMArr[self.endNum];
            }
            
            HYReservedDetailView *reservedDetailView = [[HYReservedDetailView alloc]initWithFrame:CGRectMake(0, startIndex*_cellHeight/2, KSCREEN_WIDTH, (endIndex -startIndex)*_cellHeight/2)];
            reservedDetailView.model = model;
            [self.reservedViewMArr addObject:reservedDetailView];
            [self.tableview addSubview:reservedDetailView];
        }
    }
    
    //初始化可预约的时间段
    self.upView.center = CGPointMake(75+_cellHeight/4, self.startNum*_cellHeight/2);
    self.downView.center = CGPointMake(KSCREEN_WIDTH-_cellHeight/4-10, self.endNum*_cellHeight/2);
    CGFloat dragViewX = 65;
    CGFloat dragViewY = self.upView.center.y;
    CGFloat dragViewW = KSCREEN_WIDTH-65;
    CGFloat dragViewH = self.downView.center.y - self.upView.center.y;
    self.dragView.frame = CGRectMake(dragViewX, dragViewY, dragViewW, dragViewH);
    
    self.reserveTimeLab.text = [NSString stringWithFormat:@"%@ %@ %@ %@",NSLocalizedString(@"reserve", nil),self.startTime,NSLocalizedString(@"to", nil),self.endTime];
}

-(void)selectDateAction{
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc] init];
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerMode = PGDatePickerModeDate;
    datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:15 * 60];
    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    datePickManager.isShadeBackgroud = YES;
    [self presentViewController:datePickManager animated:false completion:nil];
}

-(void)confirmBtnAction{
    if ([self isTimeOutFourHours]) {
        return;
    }
    if (self.scheduleType == MeetingRoomSchedule) {
        HYConfirmBookVC *confirmBookVC = [HYConfirmBookVC new];
        confirmBookVC.meetingDate = self.meetingDate;
        confirmBookVC.startTime = self.startTime;
        confirmBookVC.endTime = self.endTime;
        confirmBookVC.roomModel = self.roomModel;
        [self.navigationController pushViewController:confirmBookVC animated:YES];
    }
    else if (self.scheduleType == SeatSchedule){
        HYSeatBookingDetailVC *seatBookingVC = [HYSeatBookingDetailVC new];
        self.seatModel.meetingDate = self.meetingDate;
        self.seatModel.startTime = self.startTime;
        self.seatModel.endTime = self.endTime;
        seatBookingVC.seatModel = self.seatModel;
        [self.navigationController pushViewController:seatBookingVC animated:YES];
    }
    else{
        kWeakSelf(weakSelf);
        [self dismissViewControllerAnimated:YES completion:^{
            if (weakSelf.SeatTimeEditBlock) {
                weakSelf.seatModel.meetingDate = weakSelf.meetingDate;
                weakSelf.seatModel.startTime = weakSelf.startTime;
                weakSelf.seatModel.endTime = weakSelf.endTime;
                weakSelf.SeatTimeEditBlock(weakSelf.seatModel);
            }
        }];
    }
}

-(void)dismissCurrentVC{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//判断是否超过了四个小时
-(BOOL)isTimeOutFourHours{
    int startNum = (int)[self.timeMArr indexOfObject:self.startTime];
    int endNum = (int)[self.timeMArr indexOfObject:self.endTime];
    if (endNum - startNum > 8) {
        [JRToast showWithText:NSLocalizedString(@"meetingIn4HourTips", nil)];
        return YES;
    }
    else{
        return NO;
    }
}

#pragma mark - TouchTableViewDelegate
-(void)tableView:(UITableView *)tableView touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSSet *allTouches = [event allTouches];    //返回与当前接收者有关的所有的触摸对象
    UITouch *touch = [allTouches anyObject];   //视图中的所有对象
    self.beginPoint = [touch locationInView:tableView];
    CGPoint upViewPoint = [self.upView.layer convertPoint:self.beginPoint fromLayer:tableView.layer];
    CGPoint downViewPoint = [self.downView.layer convertPoint:self.beginPoint fromLayer:tableView.layer];
    CGPoint dragViewPoint = [self.dragView.layer convertPoint:self.beginPoint fromLayer:tableView.layer];
    CGPoint tableViewPoint = [self.tableview.layer convertPoint:self.beginPoint fromLayer:tableView.layer];
    
    //计算最大、最小的可移动范围
    CGFloat currentPointY = self.beginPoint.y;
    int num = currentPointY/35 <= 0 ? 0 : currentPointY/35;
    _minIndex = 0;
    _maxIndex = 48;
    
    for (NSDictionary *dic in self.reservedIndexMArr) {
        int startIndex = [dic[@"startIndex"] intValue];
        int endIndex = [dic[@"endIndex"] intValue];
        if (num >= endIndex && endIndex >= _minIndex){
            _minIndex = endIndex;
        }
        if (num <= startIndex && startIndex <= _maxIndex){
            _maxIndex = startIndex;
        }
    }
    
    if ([self.upView.layer containsPoint:upViewPoint]) {
        NSLog(@"点击了upview");
        self.touchType = InUpView;
        self.touchViewPoint = self.upView.center;
    }
    else if ([self.downView.layer containsPoint:downViewPoint]){
        NSLog(@"点击了downView");
        self.touchType = InDownView;
        self.touchViewPoint = self.downView.center;
    }
    else if ([self.dragView.layer containsPoint:dragViewPoint]){
        NSLog(@"点击了dragView");
        self.touchType = InDragView;
        self.touchViewPoint = self.dragView.center;
    }
    else if ([self.tableview.layer containsPoint:tableViewPoint]){
        NSLog(@"点击了tableView");
        self.touchType = InTableView;
        self.touchViewPoint = self.tableview.contentOffset;
    }
    else {
        NSLog(@"点击了其他的view");
        self.touchType = InOtherView;
    }
   
}

-(void)tableView:(UITableView *)tableView touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint currentPoint = [touch locationInView:tableView];
    CGFloat offsetY = currentPoint.y - self.beginPoint.y;
    CGFloat offsetCenterY = self.touchViewPoint.y + offsetY;
    CGFloat height = _cellHeight/2;
    switch (self.touchType) {
        case InUpView:
        {
            if (offsetCenterY > (self.downView.center.y - height)) {
                offsetCenterY = self.downView.center.y - height;
            }
            CGFloat upViewMinY = _minIndex*35;
            if (offsetCenterY < upViewMinY){
                offsetCenterY = upViewMinY;
            }
            self.upView.center = CGPointMake(75+_cellHeight/4, offsetCenterY);
            CGFloat dragViewX = 65;
            CGFloat dragViewY = self.upView.center.y;
            CGFloat dragViewW = KSCREEN_WIDTH-65;
            CGFloat dragViewH = self.downView.center.y - self.upView.center.y;
            self.dragView.frame = CGRectMake(dragViewX, dragViewY, dragViewW, dragViewH);
        }
            break;
        case InDownView:
        {
            if (offsetCenterY < (self.upView.center.y + height)) {
                offsetCenterY = self.upView.center.y + height;
            }
            CGFloat downViewMinY = _maxIndex*35;
            if (offsetCenterY > downViewMinY){
                offsetCenterY = downViewMinY;
            }
            self.downView.center = CGPointMake(KSCREEN_WIDTH-_cellHeight/4-10, offsetCenterY);
            CGFloat dragViewX = 65;
            CGFloat dragViewY = self.upView.center.y;
            CGFloat dragViewW = KSCREEN_WIDTH-65;
            CGFloat dragViewH = self.downView.center.y - self.upView.center.y;
            self.dragView.frame = CGRectMake(dragViewX, dragViewY, dragViewW, dragViewH);
        }
            break;
        case InDragView:
        {
            CGFloat dragViewY = self.touchViewPoint.y + offsetY;
            CGFloat dragViewX = (self.downView.center.x - self.upView.center.x)/2 + self.upView.center.x;
            CGFloat dragViewMinY = self.dragView.frame.size.height/2 + _minIndex*35;
            CGFloat dragViewMaxY = _maxIndex*35 - self.dragView.frame.size.height/2;
            
            //判断是否移出最小值
            if (dragViewY < dragViewMinY){
                dragViewY = dragViewMinY;
            }
            else if (dragViewY > dragViewMaxY){
                dragViewY = dragViewMaxY;
            }
            //重新规划坐标
            self.dragView.center = CGPointMake(dragViewX, dragViewY);
            self.upView.center = CGPointMake(75+_cellHeight/4, self.dragView.mj_y);
            self.downView.center = CGPointMake(KSCREEN_WIDTH-_cellHeight/4-10, CGRectGetMaxY(self.dragView.frame));
        }
            break;
        case InTableView:
            break;
        case InOtherView:
            break;
            
        default:
            break;
    }
}

-(void)tableView:(UITableView *)tableView touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint currentPoint = [touch locationInView:tableView];
    CGFloat offsetY = currentPoint.y - self.beginPoint.y;
    CGFloat currentPointY = currentPoint.y;
    
    int num = currentPointY/35 < 0 ? 0 : (currentPointY/35 > 48 ? 48 : currentPointY/35);
    CGFloat checkPointY = num * 35;
    CGFloat height = _cellHeight/2;
    
    switch (self.touchType) {
        case InUpView:
        {
            //upview不可大于最大值
            if ((self.touchViewPoint.y + offsetY) > (self.downView.center.y - height)) {
                checkPointY = self.downView.center.y - height;
            }
            //upview不可小于最小值
            CGFloat upViewMinY = _minIndex*35;
            if (checkPointY < upViewMinY){
                checkPointY = upViewMinY;
            }
            self.upView.center = CGPointMake(75+_cellHeight/4, checkPointY);
            CGFloat dragViewX = 65;
            CGFloat dragViewY = self.upView.center.y;
            CGFloat dragViewW = KSCREEN_WIDTH-65;
            CGFloat dragViewH = self.downView.center.y - self.upView.center.y;
            self.dragView.frame = CGRectMake(dragViewX, dragViewY, dragViewW, dragViewH);
        }
            break;
        case InDownView:
        {
            //downview不可小于最小值
            if ((self.touchViewPoint.y + offsetY) < (self.upView.center.y + height)) {
                checkPointY = self.upView.center.y + height;
            }
            //downview不可大于最大值
            CGFloat downViewMinY = _maxIndex*35;
            if (checkPointY > downViewMinY){
                checkPointY = downViewMinY;
            }
            self.downView.center = CGPointMake(KSCREEN_WIDTH-_cellHeight/4-10, checkPointY);
            CGFloat dragViewX = 65;
            CGFloat dragViewY = self.upView.center.y;
            CGFloat dragViewW = KSCREEN_WIDTH-65;
            CGFloat dragViewH = self.downView.center.y - self.upView.center.y;
            self.dragView.frame = CGRectMake(dragViewX, dragViewY, dragViewW, dragViewH);
        }
            break;
        case InDragView:
        {
            CGFloat dragViewY = self.touchViewPoint.y + offsetY;
            CGFloat dragViewX = (self.downView.center.x - self.upView.center.x)/2 + self.upView.center.x;
            CGFloat dragViewMinY = self.dragView.frame.size.height/2 + _minIndex*35;
            CGFloat dragViewMaxY = _maxIndex*35 - self.dragView.frame.size.height/2;
            
            //判断是否移出最小值
            if (dragViewY < dragViewMinY) {
                dragViewY = dragViewMinY;
            }
            else if (dragViewY > dragViewMaxY) {
                dragViewY = dragViewMaxY;
            }
            /*整点校验
             (移动后的dragView的y - dragView的一半高度) = upview校验前的centerY
             upview的centerY/30四舍五入 * 30 = upview校验后的centerY
             upview校验后的centerY + dragView的一半高度 = dragView校验后的centerY
             */
            dragViewY = round((dragViewY-self.dragView.height/2)/height) * height + self.dragView.height/2;
            
            self.dragView.center = CGPointMake(dragViewX, dragViewY);
            self.upView.center = CGPointMake(75+_cellHeight/4, self.dragView.mj_y);
            self.downView.center = CGPointMake(KSCREEN_WIDTH-_cellHeight/4-10, CGRectGetMaxY(self.dragView.frame));
        }
            break;
        case InTableView:
        {
            if (offsetY == 0 && currentPointY > 0 && currentPointY < self.tableview.contentSize.height) {
                self.upView.center = CGPointMake(75+_cellHeight/4, checkPointY);
                self.downView.center = CGPointMake(KSCREEN_WIDTH-_cellHeight/4-10, checkPointY+height);
                CGFloat dragViewX = 65;
                CGFloat dragViewY = self.upView.center.y;
                CGFloat dragViewW = KSCREEN_WIDTH-65;
                CGFloat dragViewH = self.downView.center.y - self.upView.center.y;
                self.dragView.frame = CGRectMake(dragViewX, dragViewY, dragViewW, dragViewH);
            }
        }
            break;
        case InOtherView:
            break;
            
        default:
            break;
    }
    
    self.startNum = self.upView.center.y/35;
    self.startTime = self.timeMArr[self.startNum];
    self.endNum = self.downView.center.y/35;
    self.endTime = self.timeMArr[self.endNum];
    self.reserveTimeLab.text = [NSString stringWithFormat:@"%@ %@ %@ %@",NSLocalizedString(@"reserve", nil),self.startTime,NSLocalizedString(@"to", nil),self.endTime];
    [self isTimeOutFourHours];
}

#pragma mark - PGDateSelectDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *selectDate = [gregorianCalendar dateFromComponents:dateComponents];
    
    self.meetingDate = [selectDate stringWithFormat:@"yyyy-MM-dd"];
    NSString *weekDay = [HYDateHandle weekStrFromDate:selectDate];
    _dateLab.text = [NSString stringWithFormat:@"%@，%@",weekDay,self.meetingDate];
}

#pragma mark -- UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.timeMArr.count/2-1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HYRoomScheduleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HYRoomScheduleCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setTime:self.timeMArr[indexPath.row*2]];
    return cell;
}

#pragma mark -- UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _cellHeight;
}

#pragma mark -- 懒加载

-(UIView *)navigationView{
    if (!_navigationView) {
        _navigationView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
        _navigationView.backgroundColor = KAPP_WHITE_COLOR;
        
        UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth, 44)];
        titleLab.text = @"工位日程表";
        titleLab.font = [UIFont boldSystemFontOfSize:17];
        titleLab.textAlignment = NSTextAlignmentCenter;
        [_navigationView addSubview:titleLab];
        
        UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(5, 27, 30, 30)];
        [backBtn setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
        [backBtn addTarget:self action:@selector(dismissCurrentVC) forControlEvents:(UIControlEventTouchUpInside)];
        [_navigationView addSubview:backBtn];
    }
    return _navigationView;
}

-(UILabel *)roomNameLab{
    if (!_roomNameLab) {
        _roomNameLab = [[UILabel alloc]init];
        _roomNameLab.textColor = [UIColor whiteColor];
        _roomNameLab.font = KFont(20);
        _roomNameLab.backgroundColor = RGBColor(53, 191, 206);
        _roomNameLab.textAlignment = NSTextAlignmentCenter;
    }
    return _roomNameLab;
}

-(UILabel *)dateLab{
    if (!_dateLab) {
        _dateLab = [[UILabel alloc]init];
        _dateLab.backgroundColor = RGBColor(75, 55, 140);
        _dateLab.textColor = [UIColor whiteColor];
        _dateLab.font = KFont(16);
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectDateAction)];
        [_dateLab addGestureRecognizer:tapGes];
        NSString *weekDay = [HYDateHandle weekStrFromDate:[NSDate date]];
        NSString *date = [HYDateHandle getCurrentDateStrYYYYMMDDWithTimeInterval:0];
        _dateLab.text = [NSString stringWithFormat:@"%@，%@",weekDay,date];
        _dateLab.userInteractionEnabled = YES;
        _dateLab.textAlignment = NSTextAlignmentCenter;
    }
    return _dateLab;
}

-(UIImageView *)searchImg{
    if (!_searchImg) {
        _searchImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"search"]];
    }
    return _searchImg;
}

-(UIButton *)confirmBtn{
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setBackgroundColor:KAPP_THEME_COLOR];
        [_confirmBtn setTitle:NSLocalizedString(@"confirm", nil) forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:KAPP_WHITE_COLOR forState:UIControlStateNormal];
        _confirmBtn.titleLabel.font = KFitFont(16);
        [_confirmBtn addTarget:self action:@selector(confirmBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _confirmBtn.layerCornerRadius = 4;
    }
    return _confirmBtn;
}

-(HYTouchTableView *)tableview{
    if (!_tableview) {
        _tableview = [[HYTouchTableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:(UITableViewStylePlain)];
        [_tableview registerClass:[HYRoomScheduleCell class] forCellReuseIdentifier:@"HYRoomScheduleCell"];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.touchDelegate = self;
    }
    return _tableview;
}

-(UIImageView *)upView{
    if (!_upView) {
        int startNum = (int)[self.timeMArr indexOfObject:self.startTime];
        _upView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _cellHeight/2, _cellHeight/2)];
        _upView.center = CGPointMake(75+_cellHeight/4, startNum*_cellHeight/2);
        _upView.image = [UIImage imageNamed:@"btn_drag"];
        _upView.layer.zPosition = 1.0f;
    }
    return _upView;
}

- (UIImageView *)downView{
    if (!_downView) {
        int endNum = (int)[self.timeMArr indexOfObject:self.endTime];
        _downView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _cellHeight / 2, _cellHeight/2)];
        _downView.center = CGPointMake(KSCREEN_WIDTH-_cellHeight/4-10, endNum*_cellHeight/2);
        _downView.image = [UIImage imageNamed:@"btn_drag"];
        _downView.layer.zPosition = 1.0f;
    }
    return _downView;
}

-(UIView *)dragView{
    if (!_dragView) {
        CGFloat moveViewY = self.upView.center.y;
        CGFloat moveViewH = self.downView.center.y - self.upView.center.y;
        _dragView = [[UIView alloc]initWithFrame:CGRectMake(65, moveViewY, KSCREEN_WIDTH-65, moveViewH)];
        _dragView.backgroundColor = [UIColor colorWithHexString:@"34BFCE"];
    }
    return _dragView;
}

-(UILabel *)reserveTimeLab{
    if (!_reserveTimeLab) {
        _reserveTimeLab = [[UILabel alloc]init];
        _reserveTimeLab.textColor = [UIColor whiteColor];
        _reserveTimeLab.font = KFont(15);
        _reserveTimeLab.text = [NSString stringWithFormat:@"%@ %@ %@ %@",NSLocalizedString(@"reserve", nil),self.startTime,NSLocalizedString(@"to", nil),self.endTime];
    }
    return _reserveTimeLab;
}

-(NSString *)meetingDate{
    if (!_meetingDate) {
        _meetingDate = [[NSDate date] stringWithFormat:@"yyyy-MM-dd"];
    }
    return _meetingDate;
}

-(NSString *)startTime{
    if (!_startTime) {
        _startTime = @"00:00";
    }
    return _startTime;
}

-(NSString *)endTime{
    if (!_endTime) {
        _endTime = @"00:30";
    }
    return _endTime;
}

-(NSMutableArray *)timeMArr{
    if (!_timeMArr) {
        _timeMArr = [NSMutableArray new];
        for (int i = 0; i < 25; i++) {
            NSString *time = [NSString stringWithFormat:@"%02d:00",i];
            NSString *time_30 = [NSString stringWithFormat:@"%02d:30",i];
            [_timeMArr addObject:time];
            [_timeMArr addObject:time_30];
        }
    }
    return _timeMArr;
}

-(NSMutableArray *)roomDataSource{
    if (!_roomDataSource) {
        _roomDataSource = [NSMutableArray new];
    }
    return _roomDataSource;
}

-(NSMutableArray *)reservedViewMArr{
    if (!_reservedViewMArr) {
        _reservedViewMArr = [NSMutableArray new];
    }
    return _reservedViewMArr;
}

-(NSMutableArray *)reservedIndexMArr{
    if (!_reservedIndexMArr) {
        _reservedIndexMArr = [NSMutableArray new];
    }
    return _reservedIndexMArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
